package com.pay.utils;

/**
 * Created by chenweiyi on 2018/1/30.
 */

import java.io.InputStream;
import java.net.URL;
/**
 * web资源路径
 * @author chenweiyi
 *
 */
public class WebResourceUtil {

    /**
     * (name传空字符串可以获得classes的路径)
     * Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.
     * The name of a resource is a '/'-separated path name that identifies the resource.
     * This method will first search the parent class loader for the resource;
     * if the parent is null the path of the class loader built-in to the virtual machine is searched.
     * That failing, this method will invoke findResource(String) to find the resource.
     * @param name
     * @return
     */
    public static URL getExtendResource(String name){
        return Thread.currentThread().getContextClassLoader().getResource(name);
    }

    /**
     * Returns an input stream for reading the specified resource.
     * The search order is described in the documentation for getResource(String).
     * @param name
     * @return
     */
    public static InputStream getExtendResourceAsStream(String name){
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
    }

    /**
     * 调用clazz的getResource方法
     * <pre>
     *  Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResource.
     Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
     If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
     Otherwise, the absolute name is of the following form:
     modified_package_name/name
     Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
     </pre>
     * @param clazz
     * @param name
     * @return
     */
    public static URL getResource(Class<?> clazz,String name){
        if(clazz == null){
            return null;
        }
        return clazz.getResource(name);
    }

    /**
     * 调用clazz的getResourceAsStream方法
     * <pre>
     * Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.
     * Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
     * If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
     * Otherwise, the absolute name is of the following form:
     * modified_package_name/name
     * Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
     * </pre>
     * @param clazz
     * @param name
     * @return
     */
    public static InputStream getResourceAsStream(Class<?> clazz,String name){
        if(clazz == null){
            return null;
        }
        return clazz.getResourceAsStream(name);
    }

    /**
     * <pre>
     * 若为tomcat项目，获取的是tomcat的bin目录
     * </pre>
     * @return
     */
    public static String getUserDir(){
        return System.getProperty("user.dir");
    }

    /**
     * 获取到classes目录
     * @return
     */
    public static String getClasspath(){
        return getExtendResource("").getPath();
    }

    /**
     * 获取WEB-INF
     * @return
     */
    public static String getWEB_INF(){
        return getClasspath().replaceFirst("classes/$", "");
    }

    /**
     * 获取root目录
     * @return
     */
    public static String getRootPath(){
        return getWEB_INF().replaceFirst("(WEB-INF|target|bin)/$", "");
    }

    /**
     * Spring 在 org.springframework.web.util 包中提供了几个特殊用途的 Servlet 监听器，
     * 正确地使用它们可以完成一些特定需求的功能。比如某些第三方工具支持通过 ${key} 的方式引用系统参数（即可以通过 System.getProperty() 获取的属性），
     * WebAppRootListener 可以将 Web 应用根目录添加到系统参数中，对应的属性名可以通过名为“webAppRootKey”的 Servlet 上下文参数指定，默认为“webapp.root”。
     */

    /**
     * <pre>
     * Set a system property to the web application root directory.
     * The key of the system property can be defined with the "webAppRootKey" context-param in web.xml. Default is "webapp.root".
     * Can be used for tools that support substitution with System.getProperty values, like log4j's "${key}" syntax within log file locations.
     * </pre>
     */



}
